package com.tank.netty.idle.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * Created with IntelliJ IDEA.
 * User: SuQiEr
 * Date:2017/5/30 0030
 * Time: 上午 10:28
 * To change this template use File | Settings | File Templates.
 */
public class IdleServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        //空闲事件
        if(evt instanceof IdleStateEvent ){
            IdleStateEvent event = (IdleStateEvent)evt;

            String eventType = null;

            switch (event.state() ){
                case READER_IDLE:
                    eventType = "读空闲";
                    break;
                case WRITER_IDLE:
                    eventType = "写空闲";
                    break;
                case ALL_IDLE:
                    eventType = "读写空闲";
                    break;
            }

            System.out.println(String.format("%s 发生  %s 事件",ctx.channel().remoteAddress(), eventType));
        }
    }
}
